/*
 * RestoreScriptEntry.java
 *
 * Created on November 1, 2007, 10:51 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.script;

import java.io.File;

/**
 *
 * @author david
 */
public class RestoreScriptEntry extends AbstractScriptEntry {

    private static final int RESTORE_IDX = ScriptEntry.PathType.Restore.ordinal();
    private static final int DELETE_IDX = ScriptEntry.PathType.Delete.ordinal();
    private File[] filePair = new File[DELETE_IDX + 1];

    /** Creates a new instance of RestoreScriptEntry */
    public RestoreScriptEntry(File restoreFile, File deleteFile) {
        filePair[RESTORE_IDX] = restoreFile;
        filePair[DELETE_IDX] = deleteFile;
    }

    protected RestoreScriptEntry(ScriptEntry.SavedState savedState, File restoreFile, File deleteFile) {
        this(restoreFile, deleteFile);
        this.savedState = savedState;
    }

    public File getPath() {
        throw new UnsupportedOperationException("RestoreScriptEntry.getPath() with no arguments is not allowed for a RestoreScriptEntry object.");
    }

    public File getPath(ScriptEntry.PathType pathType) {
        if (pathType == null) {
            throw new IllegalArgumentException("RestoreScriptEntry.getPath() received a null for the PathType as its parameter.");
        }
        return filePair[pathType.ordinal()];
    }

    public ScriptEntry.PathType getPathType() {
        throw new UnsupportedOperationException("RestoreScriptEntry.getPathType() with no arguments is not allowed for a RestoreScriptEntry object.");
    }

    public ScriptEntry.PathType getPathType(File path) {
        if (path == null) {
            throw new IllegalArgumentException("RestoreScriptEntry.getPathType() received a null for the File as its parameter.");
        }
        ScriptEntry.PathType pathType = null;

        if (filePair[RESTORE_IDX].toString().equals(path.toString())) {
            pathType = ScriptEntry.PathType.Restore;
        } else if (filePair[DELETE_IDX].toString().equals(path.toString())) {
            pathType = ScriptEntry.PathType.Delete;
        }
        return pathType;
    }

    // When comparing equality, don't compare the value of the saved state because this method will be called
    // for instance, when determining if a RestoreScriptEntry object's saved state should be set to 'Remove'.
    @Override
    public boolean equals(Object anObject) {
        boolean equal = false;
        if (anObject != null) {
            if (this == anObject) {
                equal = true;
            } else if (anObject instanceof RestoreScriptEntry) {
                RestoreScriptEntry rse = (RestoreScriptEntry) anObject;
                equal = rse.filePair[RESTORE_IDX].equals(this.filePair[RESTORE_IDX]) &&
                        rse.filePair[DELETE_IDX].equals(this.filePair[DELETE_IDX]);
            }
        }
        return equal;
    }

    public void setPath(File path) {
        // RestoreScriptEntry cannot support this method because there are two paths in a RestoreScriptEntry,
        // a restore path and a delete path. So, to set the path, you must specify what type of path it is.
        throw new UnsupportedOperationException("RestoreScriptEntry.setPath(File path) is not allowed " +
                "for a RestoreScriptEntry because a RestoreScriptEntry has a restore path and a delete path, " +
                "so you must specify what type of path it is.");
    }

    public void setPath(ScriptEntry.PathType pathType, File path) {
        if (pathType == null) {
            throw new IllegalArgumentException("RestoreScriptEntry.setPath() received a null for the PathType as its parameter.");
        }
        if (path == null) {
            throw new IllegalArgumentException("RestoreScriptEntry.setPath() received a null for the File as its parameter.");
        }

        if (pathType == ScriptEntry.PathType.Normal || pathType == ScriptEntry.PathType.Script) {
            throw new UnsupportedOperationException("Restore scripts cannot have their file paths set to the type of " +
                    pathType.toString());
        }
        int idx = pathType.ordinal();
        if (!filePair[idx].equals(path)) {
            filePair[idx] = path;
            savedState = ScriptEntry.SavedState.Added;
        }
    }

    public void setPathType(ScriptEntry.PathType pathType) {
        // RestoreScriptEntry cannot support this method because there are two path types in a RestoreScriptEntry,
        // a restore path type and a delete path type. So, to set the path type, you must specify what path it is.
        throw new UnsupportedOperationException("RestoreScriptEntry.setPathType(ScriptEntry.PathType pathType) " +
                "is not allowed for a RestoreScriptEntry because a RestoreScriptEntry has a restore path type and a " +
                "delete path type, so you must specify what the path is.");
    }
}

